package bg.btanev.eightyfour.client.presenter;

import bg.btanev.eightyfour.client.service.GWTCommandServiceAsync;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;

public class SearchTargetPresenter implements Presenter {

	public static interface SearchTargetDisplay extends Display {
		HasText getFirstNameData();

		HasText getLastNameData();

		HasText getEmailData();

		HasClickHandlers getSearchButton();

		void clearView();
	}

	@SuppressWarnings("unchecked")
	private final GWTCommandServiceAsync rpcService;
	private final SearchTargetDisplay display;

	@SuppressWarnings("unchecked")
	public SearchTargetPresenter(GWTCommandServiceAsync rpcService, SearchTargetDisplay display) {
		this.rpcService = rpcService;
		this.display = display;
	}

	public String getFirstName() {
		String fName = this.display.getFirstNameData().getText();
		if (fName != null) {
			if ((fName = fName.trim()).length() == 0) {
				fName = null;
			}
		}
		return fName;
	}

	public String getLastName() {
		String lName = this.display.getLastNameData().getText();
		if (lName != null) {
			if ((lName = lName.trim()).length() == 0) {
				lName = null;
			}
		}
		return lName;
	}

	public String getEmail() {
		String email = this.display.getEmailData().getText();
		if (email != null) {
			if ((email = email.trim()).length() == 0) {
				email = null;
			}
		}
		return email;
	}

	public HasClickHandlers getSearchRequest() {
		return display.getSearchButton();
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		display.clearView();
		container.add(display.asWidget());
	}
}
